<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
  
      <el-form-item label="手机号" prop="mobile">
        <el-input
          v-model="queryParams.mobile"
          placeholder="请输入手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input
          v-model="queryParams.email"
          placeholder="请输入邮箱"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
       <el-form-item label="证件类型" prop="idType">
        <el-select v-model="queryParams.idType" placeholder="请选择证件类型">
          <el-option
            v-for="item in idTypeDict"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
      </el-select>
      </el-form-item>
      <el-form-item label="证件号码" prop="idCode">
        <el-input
          v-model="queryParams.idCode"
          placeholder="请输入证件号码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="申请时间" prop="applyDate">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >审核</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="volunteerList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="ID" align="center" prop="code" /> -->
      <el-table-column label="姓名" align="center" prop="name" />
      <el-table-column :formatter="sexFormatter" label="性别" align="center" prop="sex" />
      <el-table-column label="年龄" align="center" prop="age" />
      <el-table-column :formatter="educationalPhaseFormatter" label="教育阶段" align="center" prop="educationalPhase" />
      <el-table-column label="就读学校" align="center" prop="school" />
      <el-table-column label="手机号" align="center" prop="mobile" />
      <!-- <el-table-column label="邮箱" align="center" prop="email" width="200" /> -->
      <el-table-column :formatter="idTypeFormatter" label="证件类型" align="center" prop="idType" />
      <el-table-column label="证件号码" align="center" prop="idCode" width="200" />
      <el-table-column label="申请时间" align="center" prop="applyDate" width="180"></el-table-column>
      <el-table-column :formatter="statusFormatter" label="状态" align="center" prop="status" />
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    
    <!-- 添加或修改志愿者对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="证件号码" prop="idCode">
          <el-radio-group v-model="form.status">
            <el-radio label="2">同意</el-radio>
            <el-radio label="3">不通过</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item v-if="form.status == '3'" label="不通过原因" prop="rejectReason">
          <el-input v-model="form.rejectReason" placeholder="请输入不通过原因" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listVolunteer, getVolunteer, delVolunteer, addVolunteer, updateVolunteer,checkVolunteer } from "@/api/system/volunteer";
import store from '@/store'

export default {
  name: "Volunteer",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 志愿者表格数据
      volunteerList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      dateRange: "",
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        code: null,
        name: null,
        sex: null,
        age: null,
        educationalPhase: null,
        school: null,
        mobile: null,
        email: null,
        idType: null,
        idCode: null,
        applyDate: null,
        status: null,
        agreeDate: null,
        rejectDate: null,
        rejectReason: null,
        isDel: null,
        version: null,
        createUserId: null,
        createUserName: null,
        updateUserId: null,
        updateUserName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },

      

      educationalPhaseDict: [
        {
          id: 1,
          name: '初中'
        },
        {
          id: 2,
          name: '高中'
        },
        {
          id: 3,
          name: '专科'
        },
        {
          id: 4,
          name: '本科'
        },
        {
          id: 5,
          name: '本科以上'
        },
      ],
      idTypeDict: [
        {
          id: 1,
          name: '身份证'
        },
        {
          id: 2,
          name: '港澳通行证'
        },
        {
          id: 3,
          name: '台湾通行证'
        },
        {
          id: 4,
          name: '护照'
        },
          {
          id: 5,
          name: '外国人永居证'
        },
      ]
    };
  },
  created() {
    this.getList();
  },
  methods: {
    sexFormatter(row, column, cellValue) {
      if(cellValue == '1') {
        return '男'
      }
      if(cellValue == '2') {
        return '女'
      }
      if(cellValue == '3') {
        return '未知'
      }
      return ''
    },
    educationalPhaseFormatter(row, column, cellValue) {
      let result = ''
      this.educationalPhaseDict.map(item => {
        if(item.id == cellValue) {
          result = item.name
        }
      })
      return result
    },
    idTypeFormatter(row, column, cellValue) {
      let result = ''
      this.idTypeDict.map(item => {
        if(item.id == cellValue) {
          result = item.name
        }
      })
      return result
    },
    statusFormatter(row, column, cellValue) {
      if(cellValue == '1') {
        return '未审核'
      }
      if(cellValue == '2') {
        return '同意'
      }
      if(cellValue == '3') {
        return '不通过'
      }
      return ''
    },

    /** 查询志愿者列表 */
    getList() {
      this.loading = true;
      if (this.dateRange) {
        this.queryParams.startDate = this.dateRange[0] + " 00:00:00";
        this.queryParams.endDate = this.dateRange[1] + " 23:59:59";
      } else {
        this.queryParams.startDate = "";
        this.queryParams.endDate = "";
      }

      listVolunteer(this.queryParams).then(response => {
        this.volunteerList = response.rows.map(item => {
          if(store.getters.isShowCard == '0' && item.idCode) {
            item.idCode = item.idCode.replace(/^(.{6})(?:\w+)(.{4})$/, "$1********$2")
            item.mobile = item.mobile.replace(/^(.{3})(?:\d+)(.{4})$/, "$1****$2")
          }
          return item
        });
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        code: null,
        name: null,
        sex: null,
        age: null,
        educationalPhase: null,
        school: null,
        mobile: null,
        email: null,
        idType: null,
        idCode: null,
        applyDate: null,
        status: "0",
        agreeDate: null,
        rejectDate: null,
        rejectReason: null,
        isDel: null,
        version: null,
        createUserId: null,
        createUserName: null,
        createTime: null,
        updateUserId: null,
        updateUserName: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = "";
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加志愿者";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getVolunteer(id).then(response => {
        this.form = response.data;
        if(this.form.status == '1') {
          this.form.status = '2'
        }
        this.open = true;
        this.title = "志愿者审核";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            checkVolunteer(this.form).then(response => {
              this.$modal.msgSuccess("提交成功");
              this.open = false;
              this.getList();
            });
          } else {
            addVolunteer(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除志愿者编号为"' + ids + '"的数据项？').then(function() {
        return delVolunteer(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/volunteer/export', {
        ...this.queryParams
      }, `volunteer_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
